package com.sptpc.learnspring.controller;

import com.sptpc.learnspring.controller.request.NewOrderRequest;
import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.model.Order;
import com.sptpc.learnspring.service.IDrinkService;
import com.sptpc.learnspring.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IDrinkService drinkService;
    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    public Order createNewOrder(@RequestBody NewOrderRequest orderRequest) {
        List<Drink> items = drinkService.getDrinkByIds(orderRequest.getItems());
        Order newOrder = new Order();
        newOrder.setCustomer(orderRequest.getCustomer());
        newOrder.setItems(items);
        newOrder.setState(0);
        newOrder = orderService.addNewOrder(newOrder);
        return newOrder;
    }
}
